package com.david.viewtest.view


import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView


class AvatarImageView(
    context: Context,
    attrs: AttributeSet,
) : AppCompatImageView(context, attrs) {
    var radius: Float? = null//非圆形头像可设置此值
    var avatarTitle: String? = null
    var paint = Paint()

    override fun setImageBitmap(bm: Bitmap?) {
        bm?.let { super.setImageBitmap(getRoundedCornerBitmap(it, radius)) }
    }

    override fun setImageDrawable(drawable: Drawable?) {
        if (drawable is BitmapDrawable) {
            super.setImageDrawable(BitmapDrawable(null, getRoundedCornerBitmap(drawable.bitmap, radius)))
        } else {
            super.setImageDrawable(drawable)
        }
    }

    override fun setImageResource(resId: Int) {
        setImageDrawable(context.getDrawable(resId))
    }

    override fun onDraw(canvas: Canvas) {
        if (drawable == null) {
            return  // couldn't resolve the URI
        }

        if (avatarTitle.isNullOrEmpty()) {
            super.onDraw(canvas)
        } else {
            paint.color = Color.parseColor("#f8f8f8")
            canvas.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), (width / 2).toFloat(), paint)
            //canvas?.drawRect(Rect(0, 0, width, height), paint)
            paint.color = Color.parseColor("#24292b")
            paint.textSize = 38f

            val textWidth = paint.measureText(avatarTitle)
            val x = width / 2f - paint.measureText(avatarTitle) / 2f
            val y = height / 2f - (paint.descent() + paint.ascent()) / 2f


            canvas.drawText(avatarTitle ?: "", x, y, paint)
        }

    }

    private fun getRoundedCornerBitmap(bitmap: Bitmap, radius: Float? = null): Bitmap? {
        return if (radius == null) {
            val tempR = bitmap.width / 2f
            getRoundedCornerBitmap(bitmap, tempR, tempR, tempR, tempR)
        } else {
            getRoundedCornerBitmap(bitmap, radius, radius, radius, radius)
        }
    }

    private fun getRoundedCornerBitmap(bitmap: Bitmap, topLeftRadius: Float, topRightRadius: Float, bottomRightRadius: Float, bottomLeftRadius: Float): Bitmap? {
        val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(output)
        //val color = -0xbdbdbe
        val paint = Paint()
        val rect = Rect(0, 0, bitmap.width, bitmap.height)
        val rectF = RectF(rect)
        paint.isAntiAlias = true
        //canvas.drawARGB(0, 0, 0, 0)
        //paint.color = color
        val path = Path()
        val radii =
            floatArrayOf(topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius)
        path.addRoundRect(rectF, radii, Path.Direction.CW)
        canvas.drawPath(path, paint)
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
        canvas.drawBitmap(bitmap, rect, rect, paint)
        return output
    }

    /**
     * 设置名字首字/首字母作为头像
     */
    fun setText(text: String) {

    }
}